// pages/car/car.js
var {getHomeGoods}=require("../../api/Home");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsList:[
            // {
            //     id:1,
            //     goodsImage:"/images/lists01.webp",
            //     goodsName:"1商品名称",
            //     goodsAddress:"广州",
            //     goodsPrice:"200",
            //     num:1,
            // },
            // {
            //     id:2,
            //     goodsImage:"/images/lists01.webp",
            //     goodsName:"2商品名称",
            //     goodsAddress:"广州",
            //     goodsPrice:"200",
            //     num:1,
            // },
        ],
        needList:[
            // {
            //     id:1,
            //     goodsImage:"/images/lists01.webp",
            //     goodsName:"1商品名称1商品名称1商品名称1商品名称1商品名称",
            //     goodsAddress:"广州",
            //     goodsPrice:"200",
            //     originalPrice:"300",
            // },
            // {
            //     id:2,
            //     goodsImage:"/images/lists02.webp",
            //     goodsName:"2商品名称",
            //     goodsAddress:"广州",
            //     goodsPrice:"200",
            //     originalPrice:"300",
            // },
            // {
            //     id:3,
            //     goodsImage:"/images/1.jpg",
            //     goodsName:"2商品名称",
            //     goodsAddress:"广州",
            //     goodsPrice:"200",
            //     originalPrice:"300",
            // },
            // {
            //     id:4,
            //     goodsImage:"/images/lists04.webp",
            //     goodsName:"2商品名称",
            //     goodsAddress:"广州",
            //     goodsPrice:"200",
            //     originalPrice:"300",
            // },
        ],
        totalData:0
    },
  // 添加到购物车
  addShopping(options) {
    let id = options.target.dataset.id;
    let index = this.data.needList.findIndex(item => item.id == id);
    let cartLists = {
        id: this.data.needList[index].id,
        goodsImage: this.data.needList[index].goodsImage,
        goodsName: this.data.needList[index].goodsName,
        goodsPrice: this.data.needList[index].goodsPrice,
        num: 1,
    };
    let goodsCarList = wx.getStorageSync('goodsCarList');
    if (goodsCarList) {
        let i = goodsCarList.findIndex(item => item.id == id);
        if (i != -1) {
            goodsCarList[i].num += 1;
        } else {
            goodsCarList.push(cartLists);
        }
        wx.setStorageSync('goodsCarList', goodsCarList);
    } else {
        wx.setStorageSync('goodsCarList', [cartLists])
    };
    this.setData({
        goodsList:wx.getStorageSync('goodsCarList')
    })
    this.getTotal();
    wx.showToast({
        title: "已添加到购物车",
        duration:2000
      })
},
    // 下单
    goPay(){
        wx.navigateTo({
          url: '/pages/pay/pay',
        })
    },
    // 回到首页
    goShopping(){
        wx.switchTab({
            url: '/pages/home/home',
        })
    },
    // 商品加一
add(options){
    let index=options.target.dataset.id;
    let num=this.data.goodsList[index].num;
    let key="goodsList["+index+"].num";
    this.setData({[key]:num+1})
this.getTotal();
},
// 商品减一
reduce(options){
    let index=options.target.dataset.id;
    let num=this.data.goodsList[index].num;
    let key="goodsList["+index+"].num";
    num=num<=1?1:num-1;
    this.setData({[key]:num})
this.getTotal();
},
// 删除商品
del(options){
 let index=options.currentTarget.dataset.id;
 this.data.goodsList.splice(index,1);
 this.setData({
     goodsList:this.data.goodsList
 })  
this.getTotal();
},
// 计算总金额
getTotal(){
    let goodsList=this.data.goodsList;
    if(goodsList){
        let data=goodsList.reduce(function(total,item){
            return total+item.num*item.goodsPrice;
        },0)
        this.setData({
            totalData:data
        })
    }
},
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
this.getTotal();
getHomeGoods().then(data=>{
    let lists=[];
   let goodslists=data.message.goods;
   goodslists.forEach(item=>{
       let {goods_id,goods_small_logo,goods_name,goods_price} = item;
       let data={
        id:goods_id,
        goodsName:goods_name,
        goodsPrice:goods_price,
        goodsImage:goods_small_logo,
        originalPrice:goods_price+100,
       };
       if(goods_small_logo){
           lists.push(data);
       }
   }) 
   this.setData({
    needList:lists
   })
})
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
// 获取数据
var getCartData=wx.getStorageSync('goodsCarList');
this.setData({
    goodsList:getCartData
})
this.getTotal();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
wx.setStorageSync('goodsCarList', this.data.goodsList)
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})